//
//  ViewController.swift
//  6.5.Providing Basic Content to a Collection View
//
//  Created by joker on 15/8/8.
//  Copyright (c) 2015年 joker. All rights reserved.
//

import UIKit

class ViewController: UICollectionViewController, UICollectionViewDataSource {

    let allSectionColors = [
        UIColor.redColor(),
        UIColor.greenColor(),
        UIColor.blueColor()
    ]
    
    override init(collectionViewLayout layout: UICollectionViewLayout) {
        super.init(collectionViewLayout: layout)
        
        collectionView?.registerClass(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "cell")
        
        collectionView?.backgroundColor = UIColor.whiteColor()
        
    }
    
    convenience required init(coder aDecoder: NSCoder) {
        
        let flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.minimumLineSpacing = 20
        flowLayout.minimumInteritemSpacing = 10
        flowLayout.itemSize = CGSize(width: 80, height: 120)
        flowLayout.scrollDirection = .Vertical
        
        flowLayout.sectionInset = UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20)
        
        self.init(collectionViewLayout: flowLayout)
    }
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return allSectionColors.count
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return Int(arc4random_uniform(21)+20)
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! UICollectionViewCell
        
        cell.backgroundColor = allSectionColors[indexPath.section]
        
        return cell
    }
}

